package com.zzzptc.lzh.bookreader;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zzzptc.lzh.bookreader.Utils.ProcessText;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ReadActivity extends AppCompatActivity {
    TextView readtxt,pagetxt;
    String bookname="";//第一种
    String filepath="/sdcard/Download/";//第二种
    ProcessText processText;
    private Button getPre,getNext;
    ScrollView scroll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        initView();
        setListener();
    }

    private void setListener() {
        View.OnClickListener listener=new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String content="";
                switch (v.getId()){
                    case R.id.getPre:
                        content=processText.getPre();
                        readtxt.setText(content);
                        getPage();
                        break;
                    case R.id.getNext:
                        content=processText.getNext();
                        readtxt.setText(content);
                        getPage();
                        break;
                }
            }
        };
        getPre.setOnClickListener(listener);
        getNext.setOnClickListener(listener);
    }

    private void initView() {
        scroll=findViewById(R.id.scroll);
        getPre=findViewById(R.id.getPre);
        getNext=findViewById(R.id.getNext);
        pagetxt=findViewById(R.id.pagetxt);
        readtxt=findViewById(R.id.readtxt);
        Intent it=getIntent();
        bookname=it.getStringExtra("bookname");
        String content=readAssetsTxt();
        File file=writeTxtToFile(content,filepath,bookname);
        processText=new ProcessText(file,0);
        content=processText.getNext();
        readtxt.setText(content);
        getPage();
    }

    private void getPage() {
        pagetxt.setText("第"+processText.currentpage+"页/共"+processText.pages+"页");//显示页码
        scroll.scrollTo(0,0);//点击下一页自动滑到顶端
    }

    private File writeTxtToFile(String content, String filepath, String bookname) {
        File file=new File(filepath+bookname);
        try {
            FileOutputStream fos=new FileOutputStream(file,false);
        //文件输出流对于不存在的文件会创建，false为不追加，也就是覆盖，true是重复
            fos.write(content.getBytes());
            fos.close();
            return  file;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String readAssetsTxt() {
        try {
            InputStream is=getAssets().open(bookname+".txt");//第一种
            //File file=new File(filepath+bookname+".txt");//第二种将txt文档存入模拟器内存中，从内存中读取数据
            //FileInputStream is=new FileInputStream(file);
            int size=is.available();
            byte[] buffer=new byte[size];
            is.read(buffer);
            is.close();//占内存.close关闭
            String text=new String(buffer,"utf-8");
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
